﻿using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using Willow.Authorization;
using Willow.IRepository.Auth;
using Willow.Model;

namespace Willow.EFRepository.Auth
{
    public class UserRepository : BaseRepository<Auth_User>, IUserRepository
    {
        public UserRepository(DbContext context)
            : base(context)
        { }

        /// <summary>
        /// 获取登陆用户信息
        /// </summary>
        /// <param name="userId">The user identifier.</param>
        /// <returns></returns>
        public LoginUser GetLoginUser(Guid userId)
        {
            var user = base.FirstOrDefault(p => p.Id == userId);
            var loginUser = new LoginUser();
            loginUser.UserId = user.Id;
            loginUser.UserName = user.UserName;
            loginUser.FullName = user.FullName;
            loginUser.LoginModules = new List<LoginModule>();
            loginUser.LoginActions = new List<LoginAction>();
            loginUser.OrganizationId = user.OrganizationId.Value;
            //添加用户的模块
            var lstModules = (from ur in Context.Set<Auth_UserRole>()
                              join rm in Context.Set<Auth_RoleModule>()
                              on ur.RoleId equals rm.RoleId
                              join m in Context.Set<Auth_Module>()
                              on rm.ModuleId equals m.Id
                              where m.IsDelete == false && ur.UserId == userId
                              select new LoginModule
                              {
                                  Name = m.Name,
                                  Id = m.Id,
                                  ParentId = m.ParentId,
                                  URL = m.URL,
                                  DisplayOrder = m.DisplayOrder ?? 0,
                                  Icon = m.Icon
                              }).ToList();
            foreach (var item in lstModules)
            {
                if (!loginUser.LoginModules.Any(p => p.Id == item.Id))
                {
                    loginUser.LoginModules.Add(item);
                }
            }
            //添加用户的功能
            var lstActions = (from ur in Context.Set<Auth_UserRole>()
                              join ra in Context.Set<Auth_RoleAction>()
                              on ur.RoleId equals ra.RoleId
                              join a in Context.Set<Auth_Action>()
                              on ra.ActionId equals a.Id
                              join ma in Context.Set<Auth_ModuleAction>()
                              on a.Id equals ma.ActionId
                              join m in Context.Set<Auth_Module>()
                              on ma.ModuleId equals m.Id
                              where ur.UserId == userId && a.IsDelete == false && m.IsDelete == false
                              select new LoginAction
                              {
                                  Name = a.Name,
                                  Id = a.Id,
                                  BelongURL = m.URL,
                                  DisplayOrder = a.DisplayOrder ?? 0,
                                  Handle = a.Handle,
                                  Icon = a.Icon,
                                  Category = a.Category.HasValue ? (ActionCategory)a.Category.Value : ActionCategory.Null
                              }).ToList();
            foreach (var item in lstActions)
            {
                if (!loginUser.LoginActions.Any(p => p.Id == item.Id))
                {
                    loginUser.LoginActions.Add(item);
                }
            }
            return loginUser;
        }
    }
}
